package leetcode.jianzhioffer;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-11-19 21:57
 **/
public class JianZhiOffer43 {

    public int countDigitOne(int n) {
        //10^k
        long mulk = 1;
        int res = 0;
        for (int k = 0; mulk <= n; k++) {
            res += n / (mulk * 10) * mulk + Math.min(Math.max(n % (mulk * 10) - mulk + 1, 0), mulk);
            mulk *= 10;
        }
        return res;
    }
}
